home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / fillBin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  39 lines

  1. //: C25:fillBin.cpp {O}
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Implementation of fillBin()
  7. #include "fillBin.h"
  8. #include "Fillable.h"
  9. #include "../C17/trim.h"
  10. #include "../require.h"
  11. #include <fstream>
  12. #include <string>
  13. #include <cstdlib>
  14. using namespace std;
  15.  
  16. void fillBin(string filename, Fillable& bin) {
  17.   ifstream in(filename.c_str());
  18.   assure(in, filename.c_str());
  19.   string s;
  20.   while(getline(in, s)) {
  21.     int comma = s.find(',');
  22.     // Parse each line into entries:
  23.     while(comma != string::npos) {
  24.       string e = trim(s.substr(0,comma));
  25.       // Parse each entry:
  26.       int colon = e.find(':');
  27.       string type = e.substr(0, colon);
  28.       double weight = 
  29.         atof(e.substr(colon + 1).c_str());
  30.       bin.addTrash(
  31.         Trash::factory(
  32.           Trash::Info(type, weight)));
  33.       // Move to next part of line:
  34.       s = s.substr(comma + 1);
  35.       comma = s.find(',');
  36.     }
  37.   }
  38. } ///:~
  39.